package com.example.jeson.stocktest.mvp.adapter;

import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.jeson.stocktest.R;
import com.example.jeson.stocktest.entity.MessageList;
import com.example.jeson.stocktest.entity.Stock;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;

/**
 * @author jeson
 */
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder> {

    private List<MessageList.Messages> myMessageList;
    private HashMap<String, Stock> mStockMap;
    private ArrayList<OnUpdateListen> mviewListenerList = new ArrayList<>();


    public MainAdapter(List<MessageList.Messages> messageList, HashMap<String, Stock> stockMap) {
        this.myMessageList = messageList;
        this.mStockMap = stockMap;
    }

    public void notifyStock() {
        for (OnUpdateListen viewListen : mviewListenerList) {
            viewListen.updateStock();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.init(position);
    }

    @Override
    public int getItemCount() {
        return myMessageList.size();
    }

    public interface OnUpdateListen {
        void updateStock();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements OnUpdateListen {

        AppCompatTextView mTitleTV;
        AppCompatTextView mSummaryTV;
        RecyclerView myStockRV;
        AppCompatButton mShareBTN;
        AppCompatTextView mLikeBTN;
        AppCompatTextView mTimeTV;
        AppCompatTextView mSourceTV;

        StockAdapter mStockAdapter;


        public MyViewHolder(View itemView) {
            super(itemView);
            mTitleTV = (AppCompatTextView) itemView.findViewById(R.id.title_tv);
            mSummaryTV = (AppCompatTextView) itemView.findViewById(R.id.summary_tv);
            myStockRV = (RecyclerView) itemView.findViewById(R.id.stock_rv);
            mShareBTN = (AppCompatButton) itemView.findViewById(R.id.share_btn);
            mLikeBTN = (AppCompatTextView) itemView.findViewById(R.id.like_tv);
            mTimeTV = (AppCompatTextView) itemView.findViewById(R.id.time_tv);
            mSourceTV = (AppCompatTextView) itemView.findViewById(R.id.source_tv);
        }

        private String getFormatedDateTime(long dateTime) {
            SimpleDateFormat localFormater = new SimpleDateFormat("HH:mm");
            localFormater.setTimeZone(TimeZone.getDefault());
            String localTime = localFormater.format(new Date(dateTime));
            return localTime;
        }

        public void init(final int position) {

            final MessageList.Messages data = myMessageList.get(position);

            mTitleTV.setText(data.Title);
            mSummaryTV.setText(data.Summary);
            mShareBTN.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(itemView.getContext(), data.ShareUrl, Toast.LENGTH_SHORT).show();
                }
            });
            mLikeBTN.setText(String.valueOf(data.LikeCount));
            mTimeTV.setText(getFormatedDateTime(data.UpdatedAt) + "来自:");
            mSourceTV.setText(data.Source);

            if (!mStockMap.isEmpty() && !data.Stocks.isEmpty()) {
                GridLayoutManager layoutManager = new GridLayoutManager(itemView.getContext(), 3);
                layoutManager.setSmoothScrollbarEnabled(true);
                myStockRV.setLayoutManager(layoutManager);
                myStockRV.setHasFixedSize(true);

                ArrayList<Stock> stockList = new ArrayList<>();
                for (MessageList.Messages.Stocks stock : data.Stocks) {
                    stockList.add(mStockMap.get(stock.Symbol));
                }
                mStockAdapter = new StockAdapter(stockList);
                myStockRV.setAdapter(mStockAdapter);
            }

        }

        @Override
        public void updateStock() {
            if (mStockAdapter != null) {
                mStockAdapter.notifyDataSetChanged();
            }

        }
    }
}
